using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

using Keydet.GsaPerDiem;
using System.Web.Script.Services;
using System.Collections.Generic;
using System.Xml;
using System.Web.Caching;

/// <summary>
/// Summary description for PerDiem
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class PerDiemService : System.Web.Services.WebService
{

    public PerDiemService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    //[WebMethod(CacheDuration=86400)]
    [WebMethod]
    public List<Rate> GetRatesByState(string state)
    {
        Manager m = new Manager();

        m.LogGadgetUsage(state, GetIPAddress());

        return m.GetRatesByState(state);
    }

    private string GetIPAddress()
    {
        string ipAddr = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (ipAddr == string.Empty || ipAddr == null)
        {
            ipAddr = Context.Request.ServerVariables["REMOTE_ADDR"];
        }

        return ipAddr;
    }

    //[WebMethod(CacheDuration=86400)]
    [WebMethod]
    public List<State> GetStates()
    {
        //throw new Exception("FORCE ERROR IN GADGET CLIENT");
        Manager m = new Manager();

        return m.GetStates();
    }

    [WebMethod]
    public UpdateInfo CheckForUpdate(string versionString)
    {   
        const string INFO_XML = "infoXml";
        string infoXmlPath = Server.MapPath("UpdateInfo.xml");        

        UpdateInfo cachedUpdateInfo = (UpdateInfo)Context.Cache[INFO_XML];

        if (cachedUpdateInfo == null)
        {
            cachedUpdateInfo = new UpdateInfo();

            using (XmlReader reader = XmlReader.Create(infoXmlPath))
            {
                reader.Read();
                reader.ReadStartElement("UpdateInfo");
                reader.ReadStartElement("LatestVersion");
                cachedUpdateInfo.LatestVersion = reader.ReadString();
                reader.ReadEndElement();
                reader.ReadStartElement("UpgradeUrl");
                cachedUpdateInfo.UpgradeUrl = reader.ReadString();                

                CacheDependency dep = new CacheDependency(infoXmlPath);
                Context.Cache.Insert(INFO_XML, cachedUpdateInfo, dep);
            }            
        }

        UpdateInfo ui = new UpdateInfo();

        if (cachedUpdateInfo.LatestVersion == versionString.Trim())
        {
            ui.IsUpdateAvailable = false;
            ui.UpgradeUrl = string.Empty;
            ui.LatestVersion = string.Empty;
        }
        else
        {
            ui.IsUpdateAvailable = true;
            ui.UpgradeUrl = cachedUpdateInfo.UpgradeUrl;
            ui.LatestVersion = cachedUpdateInfo.LatestVersion;
        }

        return ui;
    }
}



